/*   The MIT License
 *   
 *   Carina Engine
 *   Copyright (c) 2013 Zdravko Velinov
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

#ifndef _CARINA_CONFIG_DRIVER_HH_
#define _CARINA_CONFIG_DRIVER_HH_

#include "carina/ast.hh"
#include "carina/carina-config-ast.hh"

namespace Carina
{
namespace Config
{
class Parser;

template<class T>
struct Extractor
{
	inline static T exec(const AST::Entry* entry)
	{
        auto value = entry->getValue();
		// That's something that the user could enter.
		if(value->getNodeType() != AST::ASTNodeInfo<AST::Value<T>>::node_enum)
			return T();
		return value->extract<Value<T>>()->getValue();
	}
};

template<>
struct Extractor<string>
{
	inline static string exec(const AST::Entry* entry)
	{
        auto value = entry->getValue();
		// That's something that the user could enter.
		if(value->getNodeType() != AST::ASTNodeInfo<StringLiteral>::node_enum)
			return string();
		return value->extract<StringLiteral>()->getValue();
	}
};

template<>
struct Extractor<const AST::List*>
{
	inline static const AST::List* exec(const AST::Entry* entry)
	{
        auto value = entry->getValue();
		// That's something that the user could enter.
		if(value->getNodeType() != AST::CE_AST_BLOCK)
			return nullptr;
		auto* body = value->extract<Block>()->getBody();
		if(!body)
			return nullptr; // entirely possible that the user has not specified anything
		return body;
	}
};

/*! \brief Finds a child with the specified name.
 *  \param list     a pointer to the list that is going to be searched.
 *  \param name     the name of the searched entry.
 *  \returns The entry that is being searched or null pointer.
 */
AST::Node* FindChild(AST::Node* list, const string& name);

/*! \brief Finds a child with the specified name.
 *  \param list     a pointer to the list that is going to be searched.
 *  \param name     the name of the searched entry.
 *  \returns The entry that is being searched or null pointer.
 */
AST::Node* FindChild(AST::List* list, const string& name);

/*! \brief Finds a child with the specified name.
 *	\param list		a pointer to the list that is going to be searched.
 *	\param name		the name of the searched entry.
 *	\returns The entry that is being searched or null pointer.
 */
const AST::Node* FindChild(const AST::Node* list, const string& name);

/*! \brief Finds a child with the specified name.
 *  \param list     a pointer to the list that is going to be searched.
 *  \param name     the name of the searched entry.
 *  \returns The entry that is being searched or null pointer.
 */
const AST::Node* FindChild(const AST::List* list, const string& name);

/*! \brief Extracts the value associated with this entry.
 *	\tparam T		the type of the extracted value.
 *	\param entry	a pointer to the node that holds the value.
 *	\returns		The value associated with this node converted to the type specified as template parameter.
 */
template<class T>
inline T ExtractValue(const AST::Entry* entry)
{
	return Extractor<T>::exec(entry);
}

template<class T>
inline T ExtractValue(const AST::Node* entry)
{
    CE_ASSERT(entry->getNodeType() == CE_CONFIG_ENTRY, "Unexpected node type");
    return Extractor<T>::exec(entry->extract<Entry>());
}

/*! \brief Inserts a new entry with a value of the specified type.
 *	\tparam T			the type of the value associated with this entry.
 *  \param parent_list	the list that is going to hold the member.
 *  \param name			the name of the entry that is being inserted.
 *	\param val			the value associated with this entry.
 *  \returns Pointer which points to the new member(assumed as being the next element in the list!). You should use this pointer for inserting new sibling of the parent list.
 */
inline AST::NodeT<AST::List>* InsertEntry(AST::NodeT<AST::List>* parent_list, string name, string val)
{
    CE_ASSERT(parent_list != nullptr, "Expected valid parent because the function might create a new list");
    AST::Node string_literal = AST::CreateNode<AST::StringLiteral>(CE_DEFAULT_LOCATION, val);
	AST::Node entry = AST::CreateNode<Entry>(CE_DEFAULT_LOCATION, name, std::move(string_literal));
	if(!*parent_list)
    {
		*parent_list = AST::CreateNodeTyped<AST::List>(CE_DEFAULT_LOCATION, CE_AST_SEMICOLON_SEPARATED_LIST, std::move(entry));
        return parent_list;
    }
	(*parent_list)->push_back(std::move(entry));
	return (*parent_list)->next(); // ASSUMES THAT THERE IS NOTHING NEXT
}

/*! \brief Inserts a new entry with a value of the specified type.
 *	\tparam T			the type of the value associated with this entry.
 *  \param parent_list	the list that is going to hold the member.
 *  \param name			the name of the entry that is being inserted.
 *	\param val			the value associated with this entry.
 *  \returns Pointer which points to the new member(assumed as being the next element in the list!). You should use this pointer for inserting new sibling of the parent list.
 */
template<class T>
inline AST::NodeT<AST::List>* InsertEntry(AST::NodeT<AST::List>* parent_list, string name, T val)
{
    CE_ASSERT(parent_list != nullptr, "Expected valid parent because the function might create a new list");
	AST::Node entry = AST::CreateNode<Entry>(CE_DEFAULT_LOCATION, name, CreateNode<Value<T>>(CE_DEFAULT_LOCATION, val));
	if(!*parent_list)
    {
		*parent_list = AST::CreateNodeTyped<List>(CE_DEFAULT_LOCATION, CE_AST_SEMICOLON_SEPARATED_LIST, std::move(entry));
        return parent_list;
    }
	(*parent_list)->push_back(std::move(entry));
	return (*parent_list)->next(); // ASSUMES THAT THERE IS NOTHING NEXT
}

/*! \brief Inserts a list entry into list, which effectively creates a block.
 *  \param parent_list	the list that is going to hold the member.
 *  \param name			the name of the entry that is being inserted.
 *	\param val			the value associated with this entry.
 *  \returns Pointer which points to the new member(assumed as being the next element in the list!). You should use this pointer for inserting new sibling of the parent list.
 */
inline AST::NodeT<AST::List>* InsertEntry(AST::NodeT<AST::List>* parent_list, string name, AST::NodeT<AST::List> val)
{
    CE_ASSERT(parent_list != nullptr, "Expected valid parent because the function might create a new list");
	AST::Node entry = AST::CreateNode<Entry>(CE_DEFAULT_LOCATION, name, CreateNode<Block>(CE_DEFAULT_LOCATION, std::move(val)));
	if(!*parent_list)
    {
		*parent_list = AST::CreateNodeTyped<List>(CE_DEFAULT_LOCATION, CE_AST_SEMICOLON_SEPARATED_LIST, std::move(entry));
        return parent_list;
    }
	(*parent_list)->push_back(std::move(entry));
	return (*parent_list)->next(); // ASSUMES THAT THERE IS NOTHING NEXT
}

class Driver: public AST::Driver
{
    friend class Parser;
public:
    Driver();
     ~Driver();

    bool parse(const string& filename);
};
}
}

#endif /* _CARINA_CONFIG_DRIVER_HH_ */